package com.foreknow.view;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;

public class Login extends JFrame {

    private JPanel contentPane;
    private JTextField textField;
    private JPasswordField passwordField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    Login frame = new Login();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public Login() {
        setTitle("\u767B\u5F55");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        contentPane.setLayout(null);

        JLabel lblUsername = new JLabel("username:");
        lblUsername.setBounds(83, 55, 54, 15);
        contentPane.add(lblUsername);

        textField = new JTextField();
        textField.setBounds(172, 52, 191, 21);
        contentPane.add(textField);
        textField.setColumns(10);

        JLabel lblPassowrd = new JLabel("passowrd:");
        lblPassowrd.setBounds(83, 107, 54, 15);
        contentPane.add(lblPassowrd);

        passwordField = new JPasswordField();
        passwordField.setBounds(172, 104, 191, 21);
        contentPane.add(passwordField);

        JButton btnLogin = new JButton("login");
        btnLogin.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                //动作
                GuestbookService service = new GuestbookServiceImpl();
                //获取到用户输入的数据(用户名和密码)
                String name = textField.getText();
                String pass = passwordField.getText();
                Guestbook gb = service.login(name,pass);
                if(gb!=null) {
                    System.out.println("login success......");
                }else {
                    System.out.println("login error......");
                }
            }
        });
        btnLogin.setBounds(88, 179, 93, 23);
        contentPane.add(btnLogin);

        JButton btnRegister = new JButton("register");
        btnRegister.setBounds(191, 179, 93, 23);
        contentPane.add(btnRegister);

        JButton btnCancel = new JButton("cancel");
        btnCancel.setBounds(294, 179, 93, 23);
        contentPane.add(btnCancel);
    }
}
